
<template>
  <div> 
     <el-button @click="changeschema()">读取schema</el-button>
      表名：<el-input placeholder="请输入表名" v-model="formSchema.tablename" style="width:130px"></el-input>  
    <ncform :form-schema="formSchema" form-name="your-form-name" v-model="formSchema.value" @submit="submit()"></ncform>
    <el-button @click="submit()">新增值</el-button>
    <el-button @click="update()">编辑值</el-button> 
  </div>
</template>
 
<script>
import axios from "axios";
export default {
  data() {
    return {
      inserttableschema: [], //用于存储表的信息
      formSchema: {
        type: "object",
        ui: {
          widgetConfig: {
            layout: "h"
          }
        },
        properties: {
          name: {
            default: "李圆圆",
            type: "string",
            ui: {
              label: "姓名",
              description: "请填写你的姓名",
              placeholder: "姓名",
              columns: 6
            },
            rules: {
              required: {
                value: true,
                errMsg: "姓名不能为空"
              }
            }
          },
          age: {
            type: "number",
            value: 16,
            ui: {
              columns: 6,
              label: "年龄",
              widget: "input-number",
              widgetConfig: {
                min: 3,
                max: 30
              }
            }
          },
          email: {
            default: "846600986@qq.com",
            type: "string",
            ui: {
              label: "邮箱",
              widget: "textarea"
            },
            rules: {
              required: true,
              email: {
                value: true,
                errMsg: "请选择正确邮箱格式"
              }
            }
          },
          school: {
            type: "array",
            value: 1,
            ui: {
              label: "毕业学校",
              columns: 6,
              widget: "select",
              widgetConfig: {
                enumSource: [
                  {
                    value: 1,
                    label: "清华大学"
                  },
                  {
                    value: 2,
                    label: "北京大学"
                  },
                  {
                    value: 3,
                    label: "家里蹲大学"
                  }
                ]
              }
            }
          },
          date: {
            type: "string",
            ui: {
              label: "出生日期",
              columns: 6,
              widget: "date-picker",
              widgetConfig: {
                type: "datetime"
              }
            },
            rules: {
              required: true
            }
          },
          evaluation: {
            type: "number",
            ui: {
              label: "评分",
              columns: 6,
              widget: "rate",
              widgetConfig: {
                allowHalf: true
              }
            }
          },
          isflag: {
            type: "boolean",
            value: true,
            ui: {
              label: "是否需要支持",
              columns: 5
            }
          },
          colors: {
            type: "array",
            ui: {
              widget: "checkbox",
              widgetConfig: {
                enumSource: [
                  {
                    value: "aa",
                    label: "red"
                  },
                  {
                    value: 2,
                    label: "green"
                  },
                  {
                    value: 3,
                    label: "blue"
                  }
                ]
              }
            }
          },
          files: {
            type: "array",
            ui: {
              widget: "upload",
              widgetConfig: {
                limit: 4,
                multiples: true,
                uploadUrl: "http://localhost:8002/Home/Uploader",
                data: {
                  obj: 123,
                  b: "sample"
                }
              }
            }
          },
          label: {
            type: "string",
            default: "这是我的签名",
            ui: {
              label: "个性签名",
              widget: "label",
              widgetConfig: {
                multiLine: true
              }
            }
          },
          user: {
            type: "array",
            items: {
              type: "object",
              properties: {
                firstname: {
                  type: "string"
                },
                lastname: {
                  type: "string"
                }
              }
            },
            ui: {
              widget: "array-table",
              widgetConfig: {
                delAllTxt: "删除全部"
              }
            }
          }
          /*  user: {
            type: "object",
            properties: {
              name: {
                default: "张三",
                type: "string",
                ui: {
                  label: "姓名",
                  columns: 3
                }
              },
              age: {
                default: 22,
                type: "int",
                ui: {
                  label: "年龄",
                  columns: 6
                }
              }
            },
            ui: {
              label: "成员",
              widgetConfig: {
                // collapsed: true
              }
            }
          },
          usertables: {
            type: "array",
            items: {
              type: "object",
              properties: {
                name: {
                  type: "string",
                  default: "王五",
                  ui: {
                    label: "姓名"
                  }
                },
                age: {
                  type: "int",
                  default: 12,
                  ui: {
                    label: "姓名"
                  }
                }
              }
            },
            ui: {
              widget: "array-tabs",
              widgetConfig: {
                tabPosition: "left",
                collapsed: true
              }
            }
          } */
        },
        value: {}
      }
    };
  },
  methods: {
    //插入值
    submit() {
      //添加主键
      this.formSchema.value["id"] = this.uuid();
      console.log(JSON.stringify(this.formSchema.value));
      axios
        .post(
          "http://localhost:3000/" + this.formSchema.tablename,
          this.formSchema.value
        )
        .then(res => {})
        .catch(function(error) {
          console.log(11 + error);
        });
    },
    update() {},
    changeschema: function() {
      axios
        .get("http://localhost:3000/schemaList")
        .then(res => {
          this.formSchema = res.data[0].ncschema;
          //console.log(this.formSchema);
        })
        .catch(function(error) {
          console.log(11 + error);
        });
    },
    uuid() {
      var s = [];
      var hexDigits = "0123456789abcdef";
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = "-";

      var uuid = s.join("");
      return uuid;
    }
  }
};
</script>
